//
//  EeMobileCodeField.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/29.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay


public class EeMobileCodeField1: EeMobileCodeField {
    
}

public class EeMobileCodeField2: EeMobileCodeField {
    override public func addLayoutRules() {
        super.addLayoutRules()
        _ = codeBt.ui.font(font: UIFont.systemFont(ofSize: 10, weight: .light)).ncolor(hex: "#fff").ntitle(title: "获取短信验证码".loc).padding(insets: [7,8]).buttonColor(hex: UIColor.main_hex).corner3
    }
    
}
public class EeMobileCodeField3: EeMobileCodeField {
    override public func addLayoutRules() {
        super.addLayoutRules()
        _ = codeBt.ui.font(font: UIFont.systemFont(ofSize: 10, weight: .light)).ncolor(hex: "#fff").ntitle(title: "获取短信验证码".loc).padding(insets: [7,8]).buttonColor(hex: UIColor.main_hex).corner3
    }
    
    
}

public class EeMobileCodeField4: EeMobileCodeField {
    override public func addLayoutRules() {
        super.addLayoutRules()
        _ = codeBt.ui.font(font: UIFont.systemFont(ofSize: 11, weight: .regular)).ncolor(hex: UIColor.main_hex).ntitle(title: "获取短信验证码".loc).buttonColor(hex: "#fff").padding(insets: [7,8]).corner3
    }
}

open class EeMobileCodeField: FTextField {
    public let codeBt = UIButton()
    public var mobileName = "mobile"
    
    public var waitingSecond = 60
    public var inWaiting = false
    public var timer:SwiftTimer?
    
    open override func addLayoutRules() {
        self.eelay = [
            [codeBt,[ee.Y.R]],
            [valueT,[ee.T.L.B,[0,0.+1000,0]],[codeBt,ee.L,ee.R,-8]]
        ]
        valueT.setContentConstrainToLowLever()
        codeBt.setContentConstrainToHighLever()
        _ = codeBt.ui.font(font: UIFont.systemFont(ofSize: 10, weight: .light)).ncolor(hex: "#fff").ntitle(title: "获取短信验证码".loc).padding(insets: [7,8]).buttonColor(hex:UIColor.main_hex).corner3
        
    }
    
    
    open override func didLayoutRules() {
        super.didLayoutRules()
        self.defaultField = StrField(id: "code", defaultV: nil, need: true, hiden: false, rule:
            JoRule(reg: "^[\\s\\S]{1,200}$", nil_msg: "请填写验证码".loc, err_msg: "请填写有效的验证码".loc))
        
        weak var wself = self
        self.timer = SwiftTimer(interval: .seconds(Int(1)), repeats: true, queue: .main, handler: { (timer) in
            wself?.waitCode()
        })
        timer?.start()
        
        codeBt.addTarget(self, action: #selector(checkCode), for: .touchUpInside)
    }
    
    
    @objc open func checkCode()
    {
        if !inWaiting
        {
            weak var wself = self
            if let tableView = (controller as? ListController)?.tableView
            {
                
                let field = JoPaw.allFields(tableView: tableView).filter {(field) -> Bool in
                    guard let ws = wself else{return false}
                    return ws.ckecks.contains(field._ID)
                    }.first
                
                if let mobile = field
                {
                    let result = JoPaw.paw(inputs: [mobile])
                    if result.validate
                    {
                        
                        weak var wself = self
                        CoHud.loading(controller?.view)
                        var values = result.values
                        if let ext = mo.model["ext"] as? NSMutableDictionary
                        {
                            if let keys = ext.allKeys as? [String]
                            {
                                for one in keys
                                {
                                    values[one] = ext["\(one)",""]
                                }
                            }
                        }
                        
                        JoTask().get.url(mo.model["url",""]).log.log_response().params(values).json_handle({ (status, msg, obj, resp) in
                            CoHud.dismiss(wself?.controller?.view)
                            if status.isSuccess{
                                wself?.inWaiting = true
                                "验证码已发送".loc.alert()
                                return
                            }
                            msg.alert()
                        }).run()
                        
                        
                    }
                }
                else{
                    return
                }
            }
        }
    }
    
    @objc open func waitCode()
    {
        if  inWaiting
        {
            waitingSecond = waitingSecond-1
            codeBt.setTitle("等待[XX]秒".loc, for: .normal)
        }
        if waitingSecond <= 0
        {
            inWaiting = false
            self.waitingSecond = 60
            codeBt.setTitle("获取验证码".loc, for: .normal)
        }
    }
    
    deinit {
        timer?.suspend()
    }
    
}


